<template>
  <el-dialog v-loading="loading" :append-to-body="true" :close-on-click-modal="false" :visible.sync="visable" :title="isAdd ? '新增角色' : '编辑角色'" width="500px" @close="closeDlg">
    <el-form ref="xform" :model="form" :rules="rules" size="small" label-width="80px">
      <el-form-item label="编号" prop="code" :error="formError.code">
        <el-input v-model="form.code" maxlength="20" :disabled="!isAdd" />
      </el-form-item>
      <el-form-item label="名称" prop="name" :error="formError.name">
        <el-input v-model="form.name" autocomplete="false" maxlength="60" />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="form.remark" autocomplete="false" maxlength="160" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { get, addRole, updateRole } from '@/api/role'
export default {
  name: 'RoleForm',
  data() {
    return {
      isAdd: false,
      loading: false,
      visable: false,
      form: {
        id: null,
        name: '',
        code: null,
        remark: ''
      },
      formError: {
        name: null,
        code: null
      },
      rules: {
        code: [
          { required: true, message: '请输入编号', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    resetFields() { // 自定义resetFields this.$refs['xform'].resetFields(),有坑。如果第一次编辑一个记录，新增时初始值是前面的编辑记录值
      this.form = {
        id: null,
        name: '',
        code: null,
        remark: ''
      }
      this.formError.name = null
      this.formError.code = null
      this.$refs['xform'].resetFields()
    },
    cancel() {
      this.closeDlg()
    },
    doSubmit() {
      this.$refs['xform'].validate((valid) => {
        if (valid) {
          this.loading = true
          if (this.isAdd) {
            this.doAdd()
          } else this.doEdit()
        }
      })
    },
    add() {
      this.isAdd = true
      this.visable = true
    },
    edit(id) {
      get(id).then(data => {
        this.form.id = data.id
        this.form.name = data.name
        this.form.code = data.code
        this.form.remark = data.remark
        this.isAdd = false
        this.visable = true
      }).catch(err => {
        this.$message.error(err.message)
      })
    },
    doAdd() {
      addRole(this.form).then(res => {
        this.closeDlg()
        this.$notify({
          title: '添加成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.$parent.getList() // refrsh
      }).catch(err => {
        this.loading = false
        this.$message.error('新增错误！' + err.message)
        // console.log(err.code)
        if (err.code === 20001) {
          this.formError.code = err.message
        } else if (err.code === 20002) {
          this.formError.name = err.message
        }
      })
    },
    doEdit() {
      updateRole(this.form).then(res => {
        this.closeDlg()
        this.$notify({
          title: '修改成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.$parent.getList()
      }).catch(err => {
        this.loading = false
        this.$message.error('修改错误！' + err.message)
        if (err.code === 20001) {
          this.formError.code = err.message
        } else if (err.code === 20002) {
          this.formError.name = err.message
        }
      })
    },
    closeDlg() {
      this.visable = false
      this.resetFields()
    }
  }
}
</script>
